/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prototipos;

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.text.BadLocationException;

/**
 *
 * @author Hugo
 */
public class Login1_panel extends javax.swing.JPanel {

    /**
     * Creates new form Login1_panel
     */
    Jogo j;
    public Login1_panel(Jogo j) {
        initComponents();
        inicialiCaixasTexto();
        this.j=j;
    }

    public void inicialiCaixasTexto(){
        tb_utilizador.setDocument(new FixaTamanhoTextBox(20));
        pf_password.setDocument(new FixaTamanhoTextBox(20));
    }
    
    public Cursor setCursor(int i){
        Image image;
        Toolkit tk = Toolkit.getDefaultToolkit();
        if(i==1)
            image = tk.getImage(getClass().getResource("/imagens/atras.png"));
        else
            image = tk.getImage(getClass().getResource("/imagens/seta2.png"));
        
        Cursor cursor = tk.createCustomCursor(image, getLocation(), "meuCursor");
        
        
        return cursor;
    }
    
    private void limparDados()
    {
        tb_utilizador.setText("");
        pf_password.setText("");
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        bt_ok = new javax.swing.JButton();
        pf_password = new javax.swing.JPasswordField();
        bt_res = new javax.swing.JButton();
        tb_utilizador = new javax.swing.JTextField(10);
        jLabel1 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(1000, 650));
        setLayout(null);

        jLabel3.setFont(new java.awt.Font("Alice in Wonderland", 0, 48)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Nome:");
        add(jLabel3);
        jLabel3.setBounds(150, 200, 220, 60);

        jLabel4.setFont(new java.awt.Font("Alice in Wonderland", 0, 48)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Senha:");
        add(jLabel4);
        jLabel4.setBounds(150, 280, 220, 50);

        bt_ok.setFont(new java.awt.Font("Alice in Wonderland", 0, 48)); // NOI18N
        bt_ok.setForeground(new java.awt.Color(255, 255, 255));
        bt_ok.setText("OK");
        bt_ok.setBorder(null);
        bt_ok.setBorderPainted(false);
        bt_ok.setContentAreaFilled(false);
        bt_ok.setCursor(setCursor(2));
        bt_ok.setFocusPainted(false);
        bt_ok.setFocusable(false);
        bt_ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_okActionPerformed(evt);
            }
        });
        add(bt_ok);
        bt_ok.setBounds(430, 500, 140, 90);

        pf_password.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
        pf_password.setPreferredSize(new java.awt.Dimension(6, 49));
        add(pf_password);
        pf_password.setBounds(380, 280, 200, 50);

        bt_res.setBorder(null);
        bt_res.setContentAreaFilled(false);
        bt_res.setCursor(setCursor(1));
        bt_res.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_resActionPerformed(evt);
            }
        });
        add(bt_res);
        bt_res.setBounds(100, 523, 60, 60);

        tb_utilizador.setFont(new java.awt.Font("Alice in Wonderland", 0, 36)); // NOI18N
        tb_utilizador.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tb_utilizadorActionPerformed(evt);
            }
        });
        tb_utilizador.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tb_utilizadorKeyPressed(evt);
            }
        });
        add(tb_utilizador);
        tb_utilizador.setBounds(380, 200, 200, 50);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/login1.png"))); // NOI18N
        add(jLabel1);
        jLabel1.setBounds(0, 0, 1000, 650);
    }// </editor-fold>//GEN-END:initComponents

    private void bt_okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_okActionPerformed
        String nome="";
        String pw1 ="";
        char[] pw = null;
        int op=0;
        
        
        nome = tb_utilizador.getText();
        pw = pf_password.getPassword();
        
        if(nome.equalsIgnoreCase("") || pw.length==0)
            JOptionPane.showMessageDialog(null, "Preencher Campos Login e Password","login", JOptionPane.OK_OPTION);
        else
        {
            
            for(int i=0;i<pw.length;i++)
            {
                pw1+=pw[i];
            }
            //JOptionPane.showMessageDialog(null, pw1+" : "+nome,"login", JOptionPane.OK_OPTION);
            if(j.getAcessoBd().verificaLogin(nome, pw1,1)==true)
            {
                limparDados();
                j.getEcra().getLogin1_panel().setVisible(false);
                j.getEcra().criaPrincipal();
                j.setEstados(new Estado_Perfil(j,j.getTipoJog())); 
                j.criaJogadorComputador();
                j.addObserver(j.getEcra().getPrincipal1_panel());//adiciona o observer a janela
            }else
            {
                op=JOptionPane.showConfirmDialog(null, "Nome: "+nome,"Confirmação do Nome", JOptionPane.YES_NO_OPTION);
                if(op==0)
                {
                    if(j.getAcessoBd().registar(nome,pw1,1)==1)
                        JOptionPane.showMessageDialog(null,"Password Inválida","Login", JOptionPane.OK_OPTION);
                    else
                    {
                        limparDados();
                        j.getEcra().getLogin1_panel().setVisible(false);
                        j.getEcra().criaPrincipal();
                        j.setEstados(new Estado_Perfil(j,j.getTipoJog()));
                        j.criaJogadorComputador();
                        j.addObserver(j.getEcra().getPrincipal1_panel());//adiciona o observer a janela
                    }   
                }else
                {
                    limparDados();
                }
                //regista jogador e segue po perfil
            }
        }
        
        
    }//GEN-LAST:event_bt_okActionPerformed

    private void tb_utilizadorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tb_utilizadorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tb_utilizadorActionPerformed

    private void tb_utilizadorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tb_utilizadorKeyPressed
        /*String num = tb_utilizador.getText();
        String texto ="";
        char aux[]=new char[num.length()];
        if(num.length()>10)
        {
          JOptionPane.showMessageDialog(null, "Campo excedeu o tamanho permitido (30 caracteres)");
          num.getChars(0,num.length()-1 , aux, 0);
          for(int i=0;i<aux.length;i++)
            texto += Character.toString(aux[i]);
          tb_utilizador.setText(texto);
        }*/
    }//GEN-LAST:event_tb_utilizadorKeyPressed

    private void bt_resActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_resActionPerformed
       //j.setEstados(new Estado_Inicio(j));
        j.getEcra().getLogin1_panel().setVisible(false);
        j.getEcra().getInicio_panel().setVisible(true);
    }//GEN-LAST:event_bt_resActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_ok;
    private javax.swing.JButton bt_res;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPasswordField pf_password;
    private javax.swing.JTextField tb_utilizador;
    // End of variables declaration//GEN-END:variables
}
